package com.gz.sevrlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gz.action.Action;
import com.gz.bean.AnnotationBean;
import com.gz.pojo.Animal;
import com.gz.utils.FullBeanUtils;
import com.gz.utils.MapUtils;

/**
 * Servlet implementation class MySevrlet
 */

public class MySevrlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MySevrlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
		
		
		
	 String	path=request.getServletPath().split("\\.")[0];
	 
	 Map<String,AnnotationBean> map= (Map<String,AnnotationBean>)this.getServletContext().getAttribute("configInit");
		 
	 AnnotationBean o=  map.get(path);
	 String beanName=o.getBeanClass();
	 Animal animal= FullBeanUtils.full(beanName, request);
	 String actionClass=o.getActionClass();
	 Action action=null;
	 String url="";
	 try{
		 Class clazz=Class.forName(actionClass);
		 action=(Action) clazz.newInstance();
		url= action.execute(request,animal,o.getActionForward());
	 }catch(Exception e){
		 e.printStackTrace();
	 }
		
	 RequestDispatcher dispatcher =request.getRequestDispatcher(url);
	 dispatcher.forward(request, response);
	 
		/*String sign = request.getParameter("sign");
		Map<String, String> map = MapUtils.returnSign();

		Action action = null;
		String str="";
		try {
			Animal o = FullBeanUtils.full(map.get(sign), request);

			Map<String, String> mapAction = MapUtils.returnSignAction();
			String acionName = mapAction.get(sign);
			Class clazz= Class.forName(acionName);
			action=(Action) clazz.newInstance();
			str=action.execute(o);

		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		response.getWriter().append(str);*/
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
